@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<!-- 引入 layui.css -->
<link href="~/css/site.css" rel="stylesheet" />
<link href="~/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form layui-form-pane" action="">
    <div style="padding: 20px;">
        <h3><i class="layui-icon layui-icon-add-1"></i> 新增工序</h3>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">工序编号</label>
            <div class="layui-input-block">
                <input type="text" name="processCode" id="processCode" autocomplete="off" class="layui-input" lay-verify="required" placeholder="系统自动生成" readonly>
            </div>
            <div class="layui-input-inline" style="width: 80px;">
                <button type="button" class="layui-btn layui-btn-sm" onclick="generateProcessCode()">生成编号</button>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">工序名称</label>
            <div class="layui-input-inline">
                <input type="text" name="processName" autocomplete="off" class="layui-input" lay-verify="required" placeholder="请输入工序分类名称">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">工序分类</label>
        <div class="layui-input-inline">
            <select name="processType_Id" id="processTypeSelect" lay-filter="processType" lay-verify="required">
                <option value="">请选择工序分类</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item" pane>
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="processStatus" value="true" title="启用" checked>
            <input type="radio" name="processStatus" value="false" title="禁用">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">负责人</label>
        <div class="layui-input-inline">
            <select name="responsible" lay-filter="responsible">
                <option value="">请选择负责人</option>
                <option value="张三">张三</option>
                <option value="李四">李四</option>
                <option value="王五">王五</option>
                <option value="赵六">赵六</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">部门</label>
        <div class="layui-input-inline">
            <select name="department" lay-filter="department">
                <option value="">请选择部门</option>
                <option value="生产部">生产部</option>
                <option value="加工部">加工部</option>
                <option value="打包部">打包部</option>
                <option value="运输部">运输部</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="remark" placeholder="请输入备注内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" lay-submit lay-filter="demo2">提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>

<script src="~/js/site.js"></script>
<script src="~/layui/layui.js"></script>

<script>
    layui.use(['form'], function () {
        var form = layui.form;
        var layer = layui.layer;
        
        // 页面加载时获取工序分类数据
        loadProcessTypes();
        
        // 页面加载完成后自动生成编号
        $(document).ready(function() {
            generateProcessCode();
        });
        
        // 自动生成工序编号
        function generateProcessCode() {
            var currentDate = new Date();
            var year = currentDate.getFullYear();
            var month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
            var day = currentDate.getDate().toString().padStart(2, '0');
            var sequence = (Math.floor(Math.random() * 9000) + 1000).toString(); // 生成4位随机序号
            var processCode = 'P' + year + month + day + sequence;
            $('#processCode').val(processCode);
        }
        
        // 加载工序分类数据
        function loadProcessTypes() {
            $.ajax({
                url: 'http://localhost:5141/api/Process/ShowProcessType',
                type: 'GET',
                data: {
                    pageindex: 1,
                    pagesize: 100, // 获取所有数据
                    processTypeCode: '',
                    processTypeName: ''
                },
                success: function (res) {
                    if (res && res.pageData && res.pageData.length > 0) {
                        var options = '<option value="">请选择工序分类</option>';
                        $(res.pageData).each(function () {
                            // 只显示启用状态的工序分类
                            if (this.processTypeStatus) {
                                options += '<option value="' + this.id + '">' + this.processTypeName + '</option>';
                            }
                        });
                        $('#processTypeSelect').html(options);
                        form.render('select'); // 重新渲染select
                    }
                },
            });
        }
        
        // 提交事件
        form.on('submit(demo2)', function (data) {
            var field = data.field; // 获取表单字段值

            var $submitBtn = $('button[lay-submit]');

            // 防止重复提交
            if ($submitBtn.prop('disabled')) {
                return false;
            }

            // 禁用提交按钮
            $submitBtn.prop('disabled', true).text('提交中...');

            // 验证必填字段
            if (!field.processCode || !field.processName || !field.processType_Id) {
                layer.msg('请填写所有必填字段', { icon: 2 });
                return false;
            }
            // 将表单数据包装成API期望的格式
            var request = {
                processCode: field.processCode.trim(),
                processName: field.processName.trim(),
                processType_Id: parseInt(field.processType_Id),
                responsible: field.responsible ? field.responsible.trim() : '',
                department: field.department ? field.department.trim() : '',
                processStatus: field.processStatus === 'true', // 转换为布尔值
                remark: field.remark ? field.remark.trim() : null
            };

            // 此处可执行 Ajax 等操作
            $.ajax({
                url: 'http://localhost:5141/api/Process/AddProcessinfo',
                type: 'POST',
                data: JSON.stringify(request),
                contentType: 'application/json',
                success: function (res) {
                    if (res && res.data > 0) {
                        layer.msg('添加成功', { icon: 1 });
                        // 清空表单
                        $('form')[0].reset();
                        form.render();
                        // 重新加载工序分类（以防有新增的分类）
                        loadProcessTypes();

                         // 如果是在容器中，可以跳转回列表页面
                            if (window.parent && window.parent.loadPage) {
                                window.parent.loadPage('/Process/ProcessList', '工序管理列表');
                            }
                    }
                    else {
                        layer.msg('添加失败', { icon: 2 });
                    }
                }
            });
            return false; // 阻止默认 form 跳转
        });
    });
</script>

